package com.kun.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.dto.SysUserDTO;
import com.kun.dto.UserDto;
import com.kun.entity.SysPermission;
import com.kun.entity.SysRole;
import com.kun.entity.SysUser;
import com.kun.enums.MenuTypeEnums;
import com.kun.service.SysMenuService;
import com.kun.service.SysRoleService;
import com.kun.service.SysUserService;
import com.kun.util.PasswordUtil;
import com.kun.util.ResponseResult;
import com.kun.util.TenantContext;
import com.kun.vo.SysUserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * @author kun.li
 */
@RestController
@RequestMapping("user")
@Tag(name = "用户管理", description = "用户管理")
public class UserController {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysMenuService sysMenuService;

    @Resource
    private SysRoleService sysRoleService;

    @GetMapping("getSysUser")
    public SysUser getSysUser(String username, String userId) {
        LambdaQueryWrapper<SysUser> queryWrapper = Wrappers.<SysUser>lambdaQuery()
                .eq(StrUtil.isNotEmpty(userId), SysUser::getUserId, userId)
                .eq(StrUtil.isNotEmpty(username), SysUser::getUsername, username);
        SysUser sysUser = sysUserService.getOne(queryWrapper);
        if (StrUtil.isNotEmpty(userId)) {
            List<SysRole> sysRoles = sysRoleService.selectSysRole(sysUser.getUserId());
            if (CollUtil.isNotEmpty(sysRoles)) {
                sysUser.setRoles(sysRoles);
            }
        }
        return sysUser;
    }

    @GetMapping("getSysUser/{tenantId}")
    public SysUser getSysUser(@PathVariable String tenantId, String username, String userId) {
        TenantContext.setTenantId(tenantId);
        LambdaQueryWrapper<SysUser> queryWrapper = Wrappers.<SysUser>lambdaQuery()
                .eq(StrUtil.isNotEmpty(userId), SysUser::getUserId, userId)
                .eq(StrUtil.isNotEmpty(tenantId),SysUser::getTenantId,tenantId)
                .eq(StrUtil.isNotEmpty(username), SysUser::getUsername, username);
        SysUser sysUser = sysUserService.getOne(queryWrapper);
        if (StrUtil.isNotEmpty(userId)) {
            List<SysRole> sysRoles = sysRoleService.selectSysRole(sysUser.getUserId());
            if (CollUtil.isNotEmpty(sysRoles)) {
                sysUser.setRoles(sysRoles);
            }
        }
        TenantContext.clear();
        return sysUser;
    }


    @GetMapping("selectPermission")
    public List<SysPermission> selectPermission(String tenantId,String userId) {
        return sysMenuService.selectPermission(MenuTypeEnums.BUTTON.getMenuType(),tenantId, userId);
    }


    @GetMapping("selectUserByDeptId")
    public List<SysUser> selectUserByDeptId(String deptIds) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysUser::getDeptId, deptIds);
        return sysUserService.list(queryWrapper);
    }

    @Operation(summary = "用户表格")
    @PostMapping("selectList")
    public ResponseResult<Page<SysUserVO>> getTableData(@RequestBody SysUserDTO sysUserDTO) {
        return ResponseResult.success(sysUserService.getUserTableData(sysUserDTO));
    }

    @Operation(summary = "用户新增")
    @PostMapping("save")
    public ResponseResult<Boolean> save(@RequestBody UserDto userDto) {
        return ResponseResult.success(sysUserService.saveUser(userDto));
    }

    @Operation(summary = "用户编辑")
    @PostMapping("update")
    public ResponseResult<Boolean> update(@RequestBody UserDto userDto) {
        return ResponseResult.success(sysUserService.updateUser(userDto));
    }

    @Operation(summary = "用户删除")
    @DeleteMapping("delete/{userIds}")
    public ResponseResult<Boolean> remove(@PathVariable Long[] userIds) {
        return ResponseResult.success(sysUserService.removeBatchByIds(Arrays.asList(userIds)));
    }

    @PutMapping("updatePassword")
    public ResponseResult<Boolean> updatePassword(String userId, String password) {
        SysUser sysUser = sysUserService.getById(userId);
        sysUser.setPassword(PasswordUtil.getInstance().encode(password));
        sysUserService.updateById(sysUser);
        return ResponseResult.success(Boolean.TRUE);
    }


}
